-
Notifications
You must be signed in to change notification settings - Fork 30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Only serve from replica when there is a request in scope #8221
Open
chris48s
wants to merge
5
commits into
master
Choose a base branch
from
db-routers20250127
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f1679a5
to
9a003a1
Compare
This comment was marked as outdated.
This comment was marked as outdated.
2d8baf0
to
f376993
Compare
f376993
to
873cc01
Compare
Right. I promise I've finished mucking about with this now |
symroe
reviewed
Feb 12, 2025
symroe
approved these changes
Feb 18, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Refs https://app.asana.com/0/1204880927741389/1208838937595665/f
I'm going to walk through each of the changes in this commit one by one.
1. b14e19a - make DB router code easier to understand
This is just a little cleanup of terminology before we start making functional changes. While I was trying to work on this code, I found myself thinking "is the main one default or principal?". Default is sometimes the primary and sometimes the replica, depending on context. I think this makes it clearer what is going on, hopefully 🤞
2. f8c429b - only serve from replica when there is a request in scope
This is the meat of it really. When we talked about this earlier, we decided an important question was: Does the request detection add much overhead? Essentially, we don't want to add something slow to every round-trip to the DB. Having read over the source of django-middleware-global-request, here's how it works...
We register a middleware, which saves the request into a thread-safe global variable.
The middleware runs once per request (or zero times, if there is no request).
The subset of code that gets executed when we call
get_request()
is:So fundamentally, it is just:
so that's the only code we're adding into every DB (read) round-trip.
I'm not worried about the performance impact of this and I don't see the need to try and "optimise" that.
3. 6631799 - explain with comments
In this commit I am just adding a few comments to explain what is going on more clearly
4. 873cc01 - always read from primary in the admin
I'm trying to get ahead of some other problems here. Basically here I am saying that if we're editing things in the admin, also read from the primary. This then guards against the case where you edit something in the admin and it looks like it hasn't updated because it hasn't replicated.
5. Next Steps
As agreed, the next thing I'm going to do is review (and mostly remove) occurrences of
.using()
andusing=
throughout the codebase, but that will happen in a second follow-up PR.